<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar> </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno })">
          {{ record.djno }}
        </a>
      </template>
      <template #summary>
        <Table.Summary fixed>
          <Table.Summary.Row>
            <Table.Summary.Cell :index="0" :col-span="2" :style="{ textAlign: 'center', fontWeight: 'bold' }"
              >合计：</Table.Summary.Cell
            >
            <Table.Summary.Cell :index="2" :col-span="5"></Table.Summary.Cell>
            <Table.Summary.Cell :index="7" :style="{ textAlign: 'right', fontWeight: 'bold' }">
              {{ totalFqty }}</Table.Summary.Cell
            >
            <Table.Summary.Cell :index="8" :style="{ textAlign: 'right', fontWeight: 'bold' }">
              {{ totalFweight }}</Table.Summary.Cell
            >
          </Table.Summary.Row>
        </Table.Summary>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <!-- <hwList @register="registerHwDrawer" @success="handleSuccess" />
    <invList @register="registerInvDrawer" @success="handleSuccess" /> -->
  </div>
</template>
<script lang="ts" setup name="ViewsWmsJhPickJhPickMainListDetails">
  import { unref, onMounted,computed } from 'vue';
  import { Table } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { jhPickMainDelete, findHandleYjhData } from '/@/api/wms/jh/pick/jhPickMain';
  import { companyTreeData } from '/@/api/sys/company';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  // import InputForm from './form.vue';
  // import hwList from './hwList.vue';
  // import invList from './invList.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('wms.jh.pick.jhPickMain');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('已拣货'),
  };

  const props = defineProps({
    // data: { type: Object, default: {} },
    selIds: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司名称'),
        field: 'company.companyCode',
        fieldLabel: 'company.companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('拣货单号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('拣货时间'),
        field: 'createDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
          valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('拣货方式'),
        field: 'type',
        component: 'Select',
        componentProps: {
          dictType: 'jh_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('拣货状态'),
        field: 'jhStatus',
        component: 'Select',
        componentProps: {
          dictType: 'jh_main_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('商品编码'),
        field: 'basInv.viewCode',
        component: 'Input',
      },
      {
        label: t('商品名称'),
        field: 'basInv.invName',
        component: 'Input',
      },
      {
        label: t('形象刊'),
        field: 'freeVO.cfree1',
        component: 'Input',
      },
      // {
      //   label: t('制单人名称'),
      //   field: 'createByName',
      //   component: 'Input',
      // },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    
    {
      title: t('拣货单号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('拣货时间'),
      dataIndex: 'createDate',
      key: 'a.createDate',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('拣货方式'),
      dataIndex: 'type',
      key: 'a.type',
      dictType: 'jh_type',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('操作人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('商品编码'),
      dataIndex: 'basInv.viewCode',
      key: 'inv.view_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('商品名称'),
      dataIndex: 'basInv.invName',
      key: 'inv.inv_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('重量'),
      dataIndex: 'fweight',
      key: 'a.fweight',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('货位'),
      dataIndex: 'cposNames',
      key: 'a.cpos_names',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('公司名称'),
      dataIndex: 'company.companyName',
      key: 'company.company_name',
      sorter: true,
      width: 190,
      align: 'left',
      // ifShow: false,
    },
    {
      title: t('形象刊'),
      dataIndex: 'freeVO.cfree1',
      key: 'a.cfree1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('累计拣货数'),
      dataIndex: 'sumJhQty',
      key: 'a.sum_jh_qty',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('拣货状态'),
      dataIndex: 'jhStatus',
      key: 'a.jh_status',
      dictType: 'jh_main_status',
      sorter: true,
      width: 130,
      align: 'center',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除拣货单主表'),
        popConfirm: {
          title: t('是否确认删除拣货单主表'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wms:jh:pick:jhPickMain:edit',
        ifShow: () => record.status == '9',
      },
    ],
  };

  const [registerHwDrawer, { openDrawer:openHwDrawer }] = useDrawer();
  const [registerInvDrawer, { openDrawer:openInvDrawer  }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource, setProps  }] = useTable({
    api: findHandleYjhData,
    beforeFetch: (params) => {
      params.selIds = props.selIds
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    actionColumn: {
      width: 160,
      actions: (record: Recordable) => [
        {
          label:'查看货位',
          onClick: handleHwList.bind(this, { id: record.id }),
          auth: 'wms:jh:pick:jhPickMain:edit',
        },
        {
          label:'查看商品',
          color: 'success',
          onClick: handleInvList.bind(this, { id: record.id }),
          auth: 'wms:jh:pick:jhPickMain:edit',
        },
      ],
    },
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleHwList(record: Recordable) {
    openHwDrawer(true, record);
  }

  function handleInvList(record: Recordable) {
    openInvDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { djno: record.djno };
    const res = await jhPickMainDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'flow_jh_pick_main', bizKey: record.id });
  }


  const totalFqty = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    return sum;
  });

  const totalFweight = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fweight || 0), 0);
    sum = sum.toFixed(4)
    return sum;
  });

  onMounted(async () => {
    setProps()
    // handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      'company.companyCode': userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      'company.companyCode': userStore.getProjecte.code,
    });
  }

  defineExpose({
    handleSuccess,
  });
</script>
